#!/usr/bin/env python
# -*- coding: utf-8 -*-

import math


dateStr="1/1/1990"
timeStr="12:00:00"
hoursArr=timeStr.split(":");
dDecimalHours = float(hoursArr[0])+(float(hoursArr[1])+ float(hoursArr[2])/60.0)/60.0;
print dDecimalHours

dateArr=dateStr.split("/");
iMonth=float(dateArr[0])
iYear=float(dateArr[2])
iDay=math.floor(float(dateArr[1]))

print iDay,"/",iMonth,"/",iYear, timeStr
liAux1 =math.floor((iMonth-14)/12);
liAux2=(1461*math.floor((iYear + 4800 + liAux1))/4) + math.floor((367*(iMonth- 2-12*liAux1))/12)- math.floor((3*((iYear + 4900+ liAux1)/100))/4)+iDay-32075;
dJulianDate=(liAux2)-0.5+dDecimalHours/24.0;
print "JulianDay=",dJulianDate


def frac(value):
    return value-int(value)

def absDiff(val1, val2):
    return abs(val1-val2)

def gregDayFromjulianDay(jd):
    z=int(jd+0.5)
    f=frac(jd+0.5)
        
    if z<2299161:
        a=z
    else:        
        g=int((z-1867216.25)/36524.25)
        a=z+1+g-int(g/4)
    
    b=a+1524
    c=int((b-122.1)/365.25)
    d=int(365.25*c)
    e=int((b-d)/30.6001)
    
    tag=b-d-int(30.6001*e)+f
    
    tagDatum=int(tag)
    
    fracTag=frac(tag)
    secondsFromMidnight=24*60*60*fracTag
    minutesFromMidnight=int(secondsFromMidnight/60)
    hoursFromMidnight=int(fracTag*24)
    minutesFromHour=minutesFromMidnight-hoursFromMidnight*60
    secondsFromMinute=secondsFromMidnight-minutesFromMidnight*60
     
    
    if e<14:
        monat = e -1
    else:
        monat =e-13
    
    if monat>2:
        jahr = c - 4716
    else:
        jahr = c - 4715
     
        
    return [[tagDatum,monat,jahr],[hoursFromMidnight,minutesFromHour,secondsFromMinute]]


def julianDayfromGregDayTime(dateDDMMYYYYtimeHH24MMSSArr):
    Tag =dateDDMMYYYYtimeHH24MMSSArr[0][0]
    Monat=dateDDMMYYYYtimeHH24MMSSArr[0][1]
    Jahr=dateDDMMYYYYtimeHH24MMSSArr[0][2]
    hour=dateDDMMYYYYtimeHH24MMSSArr[1][0]
    minute=dateDDMMYYYYtimeHH24MMSSArr[1][1]
    second=dateDDMMYYYYtimeHH24MMSSArr[1][2]
    secondsFromMidnight=float(((hour-12)*60+minute)*60+second)    
    dayInSeconds=24*60*60
    fracDaysFromMidnight=secondsFromMidnight/dayInSeconds  
    a = int ((14.0-Monat)/12.0)
    y = Jahr+4800-a
    m = Monat +12*a -3  
    jdn = Tag + int((153.0*m+2.0)/5.0)+365*y+int(y/4)-int(y/100)+int(y/400)-32045+fracDaysFromMidnight
    return jdn


startJulDay=2447892.5

def printForthAndBack(dateDDMMYYYYtimeHH24MMSSArr):
    shortDateStr=dateDDMMYYYYtimeHH24MMSSArr
    jd=julianDayfromGregDayTime(shortDateStr)
    back2Gregory=gregDayFromjulianDay(jd)
    print shortDateStr, "->",jd,"->",back2Gregory

print gregDayFromjulianDay(2299161.0)
print gregDayFromjulianDay(2299162.0)
print gregDayFromjulianDay(2299162.5)

startJD=2299162.0
jdMapped=0
for idx in range(0,20000):
    jd=startJD+idx
    oldjdMapped=jdMapped
    jdMapped=julianDayfromGregDayTime(gregDayFromjulianDay(jd))
    #print gregDayFromjulianDay(jd)
    if absDiff(jd,jdMapped)>0.0000001 or jdMapped-oldjdMapped!=1.0:
        print jd, jdMapped
     

print julianDayfromGregDayTime(gregDayFromjulianDay(2299162.5))

printForthAndBack([[15,10,1582],[12,0,0]])
printForthAndBack([[1,1,1990],[12,0,0]])
printForthAndBack([[1,1,2000],[12,0,0]])
printForthAndBack([[30,3,2014],[12,0,0]])
printForthAndBack([[16,3,2014],[9,0,0]])
